/*   1:    */ package org.intellij.images.vfs;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.reference.SoftReference;
import com.intellij.util.LogicalRoot;
import com.intellij.util.LogicalRootsManager;
import org.apache.sanselan.ImageReadException;
import org.apache.sanselan.common.byteSources.ByteSourceArray;
import org.apache.sanselan.formats.ico.IcoImageParser;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */ 
/*  26:    */ public final class IfsUtil
/*  27:    */ {
/*  28:    */   public static final String ICO_FORMAT = "ico";
/*  29: 54 */   private static final Key<Long> a = Key.create("Image.timeStamp");
/*  30: 55 */   private static final Key<String> b = Key.create("Image.format");
/*  31: 56 */   private static final Key<SoftReference<BufferedImage>> c = Key.create("Image.bufferedImage");
/*  32: 57 */   private static final IcoImageParser d = new IcoImageParser();
/*  33:    */   
/*  34:    */   private static boolean a(@NotNull VirtualFile paramVirtualFile)
/*  35:    */     throws IOException
/*  36:    */   {
/*  37: 67 */     if (paramVirtualFile == null) {
/*  38: 67 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/images/vfs/IfsUtil", "refresh" }));
/*  39:    */     }
/*  40: 67 */     Long localLong = (Long)paramVirtualFile.getUserData(a);
/*  41: 68 */     SoftReference localSoftReference = (SoftReference)paramVirtualFile.getUserData(c);
/*  42: 69 */     if ((localLong == null) || (localLong.longValue() != paramVirtualFile.getTimeStamp()) || (SoftReference.dereference(localSoftReference) == null)) {
/*  43:    */       try
/*  44:    */       {
/*  45: 71 */         byte[] arrayOfByte = paramVirtualFile.contentsToByteArray();
/*  46: 73 */         if ("ico".equalsIgnoreCase(paramVirtualFile.getExtension())) {
/*  47:    */           try
/*  48:    */           {
/*  49: 75 */             BufferedImage localBufferedImage1 = d.getBufferedImage(new ByteSourceArray(arrayOfByte), null);
/*  50: 76 */             paramVirtualFile.putUserData(b, "ico");
/*  51: 77 */             paramVirtualFile.putUserData(c, new SoftReference(localBufferedImage1));
/*  52: 78 */             return true;
/*  53:    */           }
/*  54:    */           catch (ImageReadException localImageReadException) {}
/*  55:    */         }
/*  56: 83 */         ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(arrayOfByte, 0, arrayOfByte.length);
/*  57: 84 */         ImageInputStream localImageInputStream = ImageIO.createImageInputStream(localByteArrayInputStream);
/*  58:    */         try
/*  59:    */         {
/*  60: 86 */           Iterator localIterator = ImageIO.getImageReaders(localImageInputStream);
/*  61: 87 */           if (localIterator.hasNext())
/*  62:    */           {
/*  63: 88 */             ImageReader localImageReader = (ImageReader)localIterator.next();
/*  64:    */             try
/*  65:    */             {
/*  66: 90 */               paramVirtualFile.putUserData(b, localImageReader.getFormatName());
/*  67: 91 */               ImageReadParam localImageReadParam = localImageReader.getDefaultReadParam();
/*  68: 92 */               localImageReader.setInput(localImageInputStream, true, true);
/*  69: 93 */               int i = localImageReader.getMinIndex();
/*  70: 94 */               BufferedImage localBufferedImage2 = localImageReader.read(i, localImageReadParam);
/*  71: 95 */               paramVirtualFile.putUserData(c, new SoftReference(localBufferedImage2));
/*  72: 96 */               boolean bool2 = true;
/*  73:    */               
/*  74: 98 */               localImageReader.dispose();
/*  75:    */               
/*  76:    */ 
/*  77:    */ 
/*  78:102 */               localImageInputStream.close();
/*  79:    */               
/*  80:    */ 
/*  81:    */ 
/*  82:106 */               return bool2;
/*  83:    */             }
/*  84:    */             finally
/*  85:    */             {
/*  86: 98 */               localImageReader.dispose();
/*  87:    */             }
/*  88:    */           }
/*  89:    */         }
/*  90:    */         finally
/*  91:    */         {
/*  92:102 */           localImageInputStream.close();
/*  93:    */         }
/*  94:    */       }
/*  95:    */       finally
/*  96:    */       {
/*  97:106 */         paramVirtualFile.putUserData(a, Long.valueOf(paramVirtualFile.getTimeStamp()));
/*  98:    */       }
/*  99:    */     }
/* 100:109 */     return false;
/* 101:    */   }
/* 102:    */   
/* 103:    */   @Nullable
/* 104:    */   public static BufferedImage getImage(@NotNull VirtualFile paramVirtualFile)
/* 105:    */     throws IOException
/* 106:    */   {
/* 107:114 */     if (paramVirtualFile == null) {
/* 108:114 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/images/vfs/IfsUtil", "getImage" }));
/* 109:    */     }
/* 110:114 */     a(paramVirtualFile);
/* 111:115 */     SoftReference localSoftReference = (SoftReference)paramVirtualFile.getUserData(c);
/* 112:116 */     return (BufferedImage)SoftReference.dereference(localSoftReference);
/* 113:    */   }
/* 114:    */   
/* 115:    */   @Nullable
/* 116:    */   public static String getFormat(@NotNull VirtualFile paramVirtualFile)
/* 117:    */     throws IOException
/* 118:    */   {
/* 119:121 */     if (paramVirtualFile == null) {
/* 120:121 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/images/vfs/IfsUtil", "getFormat" }));
/* 121:    */     }
/* 122:121 */     a(paramVirtualFile);
/* 123:122 */     return (String)paramVirtualFile.getUserData(b);
/* 124:    */   }
/* 125:    */   
/* 126:    */   public static String getReferencePath(Project paramProject, VirtualFile paramVirtualFile)
/* 127:    */   {
/* 128:126 */     LogicalRoot localLogicalRoot = LogicalRootsManager.getLogicalRootsManager(paramProject).findLogicalRoot(paramVirtualFile);
/* 129:127 */     if (localLogicalRoot != null) {
/* 130:128 */       return a(paramVirtualFile, localLogicalRoot.getVirtualFile());
/* 131:    */     }
/* 132:131 */     ProjectFileIndex localProjectFileIndex = ProjectRootManager.getInstance(paramProject).getFileIndex();
/* 133:132 */     VirtualFile localVirtualFile1 = localProjectFileIndex.getSourceRootForFile(paramVirtualFile);
/* 134:133 */     if (localVirtualFile1 != null) {
/* 135:134 */       return a(paramVirtualFile, localVirtualFile1);
/* 136:    */     }
/* 137:137 */     VirtualFile localVirtualFile2 = localProjectFileIndex.getContentRootForFile(paramVirtualFile);
/* 138:138 */     if (localVirtualFile2 != null) {
/* 139:139 */       return a(paramVirtualFile, localVirtualFile2);
/* 140:    */     }
/* 141:142 */     return paramVirtualFile.getPath();
/* 142:    */   }
/* 143:    */   
/* 144:    */   private static String a(VirtualFile paramVirtualFile1, VirtualFile paramVirtualFile2)
/* 145:    */   {
/* 146:146 */     if (paramVirtualFile2.equals(paramVirtualFile1)) {
/* 147:147 */       return paramVirtualFile1.getPath();
/* 148:    */     }
/* 149:149 */     return "/" + VfsUtilCore.getRelativePath(paramVirtualFile1, paramVirtualFile2, '/');
/* 150:    */   }
/* 151:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.images.vfs.IfsUtil

 * JD-Core Version:    0.7.0.1

 */